package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
)

func main() {

	fields:=[]string{
		"category_id","supplier_ext_id","currency","sku"}
	skus:=[]string{"WIMAX700"}


	url := "https://oms.cht-group.net/api/base/inner/get_sku_list_by_sku"
	req:= GetSku{
		Skus: skus,
		WarehouseId: 46,
		Fields: fields,
	}
	// 这里添加数据
	body := sendUrl(&req, url, "POST")
	newRes := NewResFile{}
	err := json.Unmarshal(body, &newRes)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println("response Body:", string(body))
	fmt.Println(newRes)
	fmt.Println(newRes.Data[req.Skus[0]].SupplierExt.Supplier.Name)
}

func sendUrl(st interface{}, url string, method string) []byte {
	requestBody := new(bytes.Buffer)
	json.NewEncoder(requestBody).Encode(st)
	req, err := http.NewRequest(method, url, requestBody)
	req.Header.Set("Content-Type", "application/json")
	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		panic(err)
	}
	defer resp.Body.Close()
	fmt.Println("response Status:", resp.Status)
	fmt.Println("response Headers:", resp.Header)
	body, _ := ioutil.ReadAll(resp.Body)
	return body
}



type GetSku struct {
	Skus []string `json:"skus"`
	WarehouseId int `json:"warehouse_id"`
	Fields []string	`json:"fields"`
}

type NewResFile struct {
	Status string `json:"status"`
	Data   map[string]skuData `json:"data"`
	Code int `json:"code"`
}

type skuData struct {
	CategoryId int `json:"category_id"`
	SupplierExtId int `json:"supplier_ext_id"`
	Currency string `json:"currency"`
	Sku string `json:"sku"`
	ContractCategoryId int `json:"contract_category_id"`
	IsMrp int `json:"is_mrp"`
	SupplierSn string `json:"supplier_sn"`
	Category categoryData `json:"category"`
	SupplierExt supplierExtData `json:"supplier_ext"`
}

type categoryData struct {
	Id int `json:"id"`
	ParentId int `json:"parent_id"`
	Name string `json:"name"`
	Sort int `json:"sort"`
}

type supplierExtData struct {
	Id int `json:"id"`
	SupplierId int `json:"supplier_id"`
	SupplierCode string `json:"supplier_code"`
	TaxType int `json:"tax_type"`
	Supplier supplierData `json:"supplier"`
}

type supplierData struct {
	Id int `json:"id"`
	SupplierNo string `json:"supplier_no"`
	Name string `json:"name"`
}